package com.zr.service.base.impl;

import java.io.File;
import java.util.List;
import java.util.UUID;
import java.util.Base64;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.io.FileInputStream;
import javax.annotation.Resource;
import com.zr.vo.base.BaseDriverPhoto;
import com.zr.dao.base.BaseDriverPhotoDao;
import org.springframework.stereotype.Service;
import com.zr.service.base.BaseDriverPhotoService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Value;

/**
 * 司机备证照片上传和回显实现类
 */
@Service
public class BaseDriverPhotoServiceImpl implements BaseDriverPhotoService {

    @Resource
    private BaseDriverPhotoDao baseDriverPhotoDao;

    @Value("${img.path}")
    private  String imgPath;

    /**
     * 上传司机备证图片信息
     * @param files
     * @return
     * @throws IOException
     * @author liutienan
     */
    @Override
    public List<String> uploadImg(MultipartFile[] files, String driverId) throws IOException {
        ArrayList<String> imgList = new ArrayList<>();
        if (files.length == 0) return null;
        //删除原来备证图片
        baseDriverPhotoDao.deleteByDriverId(driverId);
        for (MultipartFile file : files) {
            //随机生成图片名称
            String imgUrl =  UUID.randomUUID().toString() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            file.transferTo(new File(imgPath + imgUrl));
            // 把图片地址加入数据库
            BaseDriverPhoto baseDriverPhoto = new BaseDriverPhoto();
            baseDriverPhoto.setDriverPhotoAddress(imgPath+imgUrl);
            baseDriverPhoto.setDriverId(driverId);
            //加入新的司机备证照片
            baseDriverPhotoDao.insertSelective(baseDriverPhoto);
            String imgBase = getImgBase(imgPath+imgUrl);
            imgList.add(imgBase);
        }
        return imgList;
    }

    /**
     * 司机备证照片列表
     * @param driverId
     * @return
     * @throws IOException
     * @author liutienan
     */
    @Override
    public List<String> showDriverPhotos(String driverId) {
        ArrayList<String> res = new ArrayList<>();
        List<BaseDriverPhoto> baseDriverPhotoList=baseDriverPhotoDao.selectByDriverId(driverId);
        for (BaseDriverPhoto ele : baseDriverPhotoList) {
            String imgBase = getImgBase(ele.getDriverPhotoAddress());
            res.add(imgBase);
        }
        return res;
    }

    /**
     * 将图片转换成Base64编码
     * @param imgFile
     * @return
     * @author liutienan
     */
    public static String getImgBase(String imgFile) {
        // 将图片文件转化为二进制流
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 图片头
        return Base64.getEncoder().encodeToString(data);
    }
}
